package com.ljx.itbbs.server.article.event.handler;

import cn.hutool.core.collection.CollUtil;
import com.ljx.itbbs.server.article.domain.enums.IncreaseExpEnum;
import com.ljx.itbbs.server.article.domain.enums.IncreaseIntegralEnum;
import com.ljx.itbbs.server.article.event.ArticlePostEvent;
import com.ljx.itbbs.server.article.service.ArticleCategoryService;
import com.ljx.itbbs.server.article.service.ArticleTagService;
import com.ljx.itbbs.server.article.service.PlateService;
import com.ljx.itbbs.server.article.service.ResourceService;
import com.ljx.itbbs.server.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description: 发布文章处理
 * @Date: 2024-03-24  13:11
 */
@Component
public class ArticlePostEventHandler {

    @Autowired
    private ArticleTagService articleTagService;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private UserService userService;

    @Autowired
    private ResourceService resourceService;

    /**
     * 保存文章标签
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = ArticlePostEvent.class, fallbackExecution = true)
    public void saveArticleTag(ArticlePostEvent event) {
        articleTagService.saveArticleTag(event.getArticleId(), event.getTagList());
    }

    /**
     * 添加板块文章数量
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = ArticlePostEvent.class, fallbackExecution = true)
    public void addPlateArticleNum(ArticlePostEvent event) {
        articleCategoryService.addArticleCategoryNum(event.getArticleCategoryId());
    }

    /**
     * 增加用户经验和积分
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = ArticlePostEvent.class, fallbackExecution = true)
    public void increaseUserExp(ArticlePostEvent event) {
        userService.increaseUserExp(event.getUserId(), IncreaseExpEnum.ARTICLE.getExp());
        userService.increaseUserIntegral(event.getUserId(), IncreaseIntegralEnum.ARTICLE.getIntegral());
        // 清除用户缓存
        userService.delUserRedisCache(event.getUserId());
    }


    /**
     * 若是存在附件就保存附件
     * @param event
     */
    @Async
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = ArticlePostEvent.class, fallbackExecution = true)
    public void updateArticleResource(ArticlePostEvent event) {
        if (CollUtil.isNotEmpty(event.getResourcesList()) && Objects.nonNull(event.getIntegral()) && Objects.nonNull(event.getIsFree())){
            resourceService.batchUpdateResource(event.getResourcesList(), event.getIsFree(), event.getIntegral(), event.getArticleId());
        }
    }
}
